package com.system.util.freemarker.template;

import com.component.domain.Content;
import com.component.service.ContentService;
import com.system.common.ContextUtils;
import com.system.config.freemarker.DirectiveHandler;
import com.system.config.freemarker.FreemarkerScan;
import com.system.config.freemarker.TemplateDirective;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Classname SubContentTemplate
 * @Description TODO
 * @Date 2023/9/24 11:58
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@FreemarkerScan("subcontent")
public class SubContentTemplate extends TemplateDirective {

    @Autowired
    private ContentService contentService;

    @Override
    public String getName() {
        return "subcontent";
    }

    @Override
    public void execute(DirectiveHandler handler) throws Exception {
        Integer contentId=handler.getInteger("contentId");
        Integer length=handler.getInteger("length");
        if(length==null){
            length=10;
        }
        Content content=contentService.getContentByNid(contentId);
        if(content.getIsencrypt()){
            String contStr= ContextUtils.decryptStr(content.getContent());
            if(contStr.length() < length){
                length=contStr.length();
            }
            contStr=contStr.substring(0,length-1);
            System.out.println(contStr);
            content.setContent(contStr);
        }
        handler.put("res",content).render();
    }
}
